package com.knife.file.domain.entity;

import com.knife.common.entity.BaseDto;
import com.knife.file.infra.constants.FileConstant;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.StringJoiner;


/**
 * 文件存储信息实体
 *
 * @author： 76875
 * @date： 2022/4/6 星期三 17:46
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class KnifeFileInfo extends BaseDto {

    public static final String FIELD_ID = "id";
    public static final String FIELD_ATTACHMENT_UUID = "attachmentUuid";
    public static final String FIELD_DIRECTORY = "directory";
    public static final String FIELD_FILE_URL = "fileUrl";
    public static final String FIELD_FILE_TYPE = "fileType";
    public static final String FIELD_FILE_NAME = "fileName";
    public static final String FIELD_FILE_SIZE = "fileSize";
    public static final String FIELD_BUCKET_NAME = "bucketName";
    public static final String FIELD_FILE_KEY = "fileKey";
    public static final String FIELD_TENANT_ID = "tenantId";
    public static final String FIELD_MD5 = "md5";
    public static final String FIELD_STORAGE_CODE = "storageCode";
    public static final String FIELD_SOURCE_TYPE = "sourceType";
    public static final String FIELD_SERVER_CODE = "serverCode";

    /**
     * mp自动扫描本主键
     */
    private Long id;
    /**
     * 附件集UUID
     */
    private String attachmentUuid;
    /**
     * 上传目录
     */
    private String directory;
    /**
     * 文件地址
     */
    private String fileUrl;
    /**
     * 文件类型
     */
    private String fileType;
    /**
     * 文件名称
     */
    private String fileName;
    /**
     * 文件大小
     */
    private Long fileSize;
    /**
     * 文件目录
     */
    private String bucketName;
    /**
     * 对象KEY
     */
    private String fileKey;
    /**
     * 租户Id
     */
    private Long tenantId;
    /**
     * 文件MD5
     */
    private String md5;
    /**
     * 存储编码
     */
    private String storageCode;
    /**
     * 来源类型
     */
    private String sourceType;
    /**
     * 服务器编码
     */
    private String serverCode;


    /**
     * 初始化文件Key
     *
     * @param suffixFileName uuid+文件名
     */
    public void initFileKey(String suffixFileName) {
        StringJoiner sj = new StringJoiner(FileConstant.KnifeFileConstant.DIRECTORY_SEPARATOR);
        if (StringUtils.isNotEmpty(this.directory)){
            sj.add(this.directory);
        }
        if (Objects.nonNull(this.tenantId)){
            sj.add(this.tenantId.toString());
        }
        if (Objects.nonNull(suffixFileName)){
            sj.add(suffixFileName);
        }
        this.fileKey = sj.toString();
    }
}
